import { getChatList, deleteChatItem, setChatUnread } from '@/api/chat'

const state = {
  chatList: [], // 对话列表
  activedContactId: null
}

const mutations = {
  SET_CHAT_LIST: (state, data) => {
    state.chatList = data
  },
  UPDATE_CHAT_ITEM: (state, chatItem) => {
    const { contactId } = chatItem
    const i = state.chatList.findIndex(item => item.contactId === contactId)
    if (i !== -1) {
      const oldChatItem = state.chatList[i]
      const newChatItem = {
        ...oldChatItem,
        ...chatItem,
        unread: chatItem.unread ? oldChatItem.unread + chatItem.unread : 0// unread为0全部置为0，否则叠加
      }
      state.chatList.splice(i, 1, newChatItem)
      if (chatItem.latestTime !== oldChatItem.latestTime) {
        state.chatList.sort((a, b) => a.latestTime > b.latestTime ? -1 : 1)
      }
    }
  },
  DELETE_CHAT_ITEM: (state, i) => {
    state.chatList.splice(i, 1)
  },
  CREATE_CHAT_ITEM: (state, payload) => {
    state.chatList.unshift(payload)
    state.chatList.sort((a, b) => a.latestTime > b.latestTime ? -1 : 1)
  },
  SET_ACTIVED_CONTACT_ID: (state, data) => {
    state.activedContactId = data
  }
}

const actions = {

  async initChatList({ commit }) {
    if (state.chatList.length > 0) return
    const { data } = await getChatList()
    commit('SET_CHAT_LIST', data)
  },

  // 更新对话列表项目
  async updateChatItem({ commit, dispatch }, { contactId, chatMsg }) {
    console.log('contactId--', contactId)
    const { type, content, createdAt, to, from } = chatMsg
    const latestMessageMap = {
      audio: '[语音]',
      image: '[图片]',
      file: '[文件]',
      text: content
    }
    const chatItem = {
      contactId,
      latestMessage: latestMessageMap[type],
      latestTime: createdAt,
      unread: state.activedContactId === to || state.activedContactId === from ? 0 : 1
    }
    const isNewChat = state.chatList.findIndex(item => item.contactId === contactId) === -1
    console.log('isNewChat----', isNewChat)
    if (isNewChat) {
      const { avatar, name, online } = await dispatch('contact/getContactInfo', contactId, { root: true })
      chatItem.avatar = avatar
      chatItem.name = name
      chatItem.online = online
      commit('CREATE_CHAT_ITEM', chatItem)
    } else {
      commit('UPDATE_CHAT_ITEM', chatItem)
    }
    return chatItem
  },
  // 删除对话列表项目
  async deleteChatItem({ commit, state }, contactId) {
    if (!contactId) return
    const i = state.chatList.findIndex(item => item.contactId === contactId)
    if (i !== -1) {
      commit('DELETE_CHAT_ITEM', i)
      deleteChatItem(contactId)
    }
  },
  async setChatUnread({ commit }, { contactId, unread }) {
    commit('UPDATE_CHAT_ITEM', { contactId, unread })
    await setChatUnread({ contactId, unread })
  },
  // 用户在线状态改变
  socket_onlineStatus({ commit }, payload) {
    console.log(`更新对话列表用户在线状态，用户:${payload.contactId}，${payload.status ? '离线' : '上线'}`)
    const { status, contactId } = payload
    commit('UPDATE_CHAT_ITEM', { contactId, online: status })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
